create procedure wp_GetTagStates
	@tag varchar(max)
as
	declare @dbid int
	declare @branchid int
	declare @table table(States int)
	declare @object int
	declare @objectstate int
	declare @tagstate int
	declare @infostate int

	select @tagstate = State from Tag where Tag = @tag

	select @infostate = max(State) from Info where State <= @tagstate
	select @branchid = BranchID from Info where State = @infostate

	select @dbid = o.DBID from Objects o
		inner join Data d on d.ObjectID = o.ID
		where d.State = @infostate

	declare c cursor for
		select ID from Objects where DBID = @dbid

	open c

	fetch next from c into @object
	while @@fetch_status = 0
	begin
		set @objectstate = null
		select @objectstate = dbo.wp_GetObjectState(@object, @branchid, @tagstate)
		if @objectstate <> -1
			insert into @table
				select @objectstate

		fetch next from c into @object
	end

	close c
	deallocate c

	select * from @table